package com.wicket.okrframework.biz.service.nb;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.wicket.okrcomponent.common.annotations.BanAuto;
import com.wicket.okrframework.biz.service.dto.common.FrameworkSceneRoleDto;
import com.wicket.okrframework.biz.service.dto.req.*;
import com.wicket.okrframework.biz.service.dto.res.BatchClearUserInfoFromRedisRespDto;
import com.wicket.okrframework.biz.service.dto.res.CheckTributPowerObjectRespDto;
import com.wicket.okrframework.biz.service.dto.res.GetUserAllPowerRespDto;
import com.wicket.okrframework.biz.service.dto.res.GetUserBasicInfoFromRedisRespDto;
import com.wicket.okrframework.common.redis.BaseUserInfoForRealRedis;
import com.wicket.okrframework.common.redis.BaseUserInfoForRedis;
import com.wicket.okrframework.common.redis.PrivacyInfoForRedis;
import com.wicket.okrframework.common.util.CommonFunctionHelper;
import com.wicket.okrframework.common.util.RedisUtil;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.wicket.okrframework.biz.service.dto.res.QueryReceiveFieldDetailRespDto;
import com.wicket.okrframework.biz.service.dto.res.QueryGetInfoFromRedisDetailRespDto;
import com.wicket.okrframework.biz.service.dto.res.QueryUserRoleRedisListRespDto;
import com.wicket.okrframework.biz.service.dto.res.QueryRoleOfPowerListRespDto;

/**
 * @author ： 手工接入方法
 * @since 2022/5/28 15:32
 *
 *
 * @version 1.0
 */
@Service
public class NbPower {
    /**
     * code:collectionRemoveDuplicateData
     * name:M去重权限分配对象(特殊方法）
     * desc:undefined
     **/

    @Resource
    private RedisUtil redisClient;

    @BanAuto
    @Trace(operationName = "M去重权限分配对象(特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public CheckTributPowerObjectRespDto checkTributPowerObject(CheckTributPowerObjectReqDto reqDto) {
        CheckTributPowerObjectRespDto retData = new CheckTributPowerObjectRespDto();
        List<String> filteredList = new ArrayList<>(reqDto.getTributPowerList());
        retData.setTributPowerList(filteredList.stream().distinct().collect(Collectors.toList()));
        return retData;
    }

    /**
     * code:getUserAllPower
     * name:M-获取用户全部权限（特殊方法）
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "M-获取用户全部权限（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public GetUserAllPowerRespDto getUserAllPower(GetUserAllPowerReqDto reqDto) {
        GetUserAllPowerRespDto retData = new GetUserAllPowerRespDto();
        GetUserBasicInfoFromRedisReqDto reqParam = new GetUserBasicInfoFromRedisReqDto();
        reqParam.setOriginalRoleMemberId(reqDto.getOriginalRoleMemberId());
        GetUserBasicInfoFromRedisRespDto userBaseInfo = getUserBasicInfoFromRedis(reqParam);
        List<FrameworkSceneRoleDto> roleList = userBaseInfo.getRoleList();
        List list = new ArrayList<>();
        List listCode = new ArrayList<>();
        for (int i = 0; i < roleList.size(); i++) {
            FrameworkSceneRoleDto roleInfoForRedis = roleList.get(i);
            List<TributPowerDto> powerCodeList = roleInfoForRedis.getTributPowerList();
            for (TributPowerDto privacyInfoForRedis : powerCodeList) {
                if (!listCode.contains(privacyInfoForRedis.getPrivacyCode())) {
                    listCode.add(privacyInfoForRedis.getPrivacyCode());
                    retData.getPowerCodeList().add(privacyInfoForRedis.getPrivacyCode());
                }
            }
        }
        return retData;
    }

    @BanAuto
    private GetUserBasicInfoFromRedisRespDto getUserBasicInfoFromRedis(GetUserBasicInfoFromRedisReqDto reqDto) {
        GetUserBasicInfoFromRedisRespDto retData = new GetUserBasicInfoFromRedisRespDto();
        String str = (String) redisClient.get("userbase-" + reqDto.getOriginalRoleMemberId());
        if (str == null || str.isEmpty()) {
            return retData;
        }
        BaseUserInfoForRealRedis redisData = JSONObject.parseObject(str, BaseUserInfoForRealRedis.class);
        BeanUtil.copyProperties(redisData, retData, "birthday");
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            if (redisData.getBirthday() != null && !redisData.getBirthday().isEmpty()) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String times = sdf.format(Long.valueOf(redisData.getBirthday()));
                date = ft.parse(times);
                retData.setBirthday(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return retData;
    }

    /**
     * code:batchClearUserInfoFromRedis
     * name:M-Redis批量清空用户信息（特殊方法）
     * desc:undefined
     **/
    @Trace(operationName = "M-Redis批量清空用户信息（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public BatchClearUserInfoFromRedisRespDto batchClearUserInfoFromRedis(BatchClearUserInfoFromRedisReqDto reqDto) {
        BatchClearUserInfoFromRedisRespDto retData = new BatchClearUserInfoFromRedisRespDto();
        if (CollUtil.isEmpty(reqDto.getBeginMemberList())) {
            return retData;
        }
        for (String oneMember : reqDto.getBeginMemberList()) {
            CommonFunctionHelper.clearUserInfoFromRedis(redisClient, oneMember);
        }
        return retData;
    }
/**
      * code:receptionService
      * name:M1-有值可操作
      * desc:undefined
      * gen by moon at 8/10/2022, 3:12:16 AM
      * 
      **/
     @Trace(operationName = "M1-有值可操作")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public QueryReceiveFieldDetailRespDto queryReceiveFieldDetail(QueryReceiveFieldDetailReqDto reqDto) {
         QueryReceiveFieldDetailRespDto retData =  BeanUtil.toBean(reqDto,QueryReceiveFieldDetailRespDto.class);
//         Field[] declaredFields = retData.getClass().getDeclaredFields();
//         for(Field field : declaredFields){
//             try {
//                 field.setAccessible(true);
//                 Object fieldValue = field.get(retData);
//                 if(fieldValue==null){
//                     if(field.getType().equals(String.class)){
//                         field.set(retData,"0");
//                     }else if(field.getType().equals(Long.class)){
//                         field.set(retData,0);
//                     }else if(field.getType().equals(Long.class)){
//                         field.set(retData,0l);
//                     }
//                 }
//             } catch (IllegalAccessException e) {
//                 e.printStackTrace();
//             }
//         }
        return retData;
      }
/**
      * code:getPublicFieldCache
      * name:M1-redis获取当前身份人员权限
      * desc:undefined
      * gen by moon at 8/10/2022, 3:13:16 AM
      * 
      **/
     @Trace(operationName = "M1-redis获取当前身份人员权限")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public QueryGetInfoFromRedisDetailRespDto queryGetInfoFromRedisDetail(QueryGetInfoFromRedisDetailReqDto reqDto) {
         QueryGetInfoFromRedisDetailRespDto retData = new QueryGetInfoFromRedisDetailRespDto();
         BaseUserInfoForRedis retInfo = CommonFunctionHelper.getUserBasicInfoFromRedis(redisClient,reqDto.getOriginalRoleMemberId());
         List<PrivacyInfoForRedis> powerList = retInfo.getPowerCodeList();
         for(PrivacyInfoForRedis onePower : powerList){
             if(onePower.getPrivacyCode().equals(reqDto.getPrivacyCode())){
                 retData.setPrivacyCode(reqDto.getPrivacyCode());
             }
         }
         return retData;
      }
/**
      * code:getUserRoleListFromRedis
      * name:M-查用户角色缓存Redis列表（特殊方法）
      * desc:undefined
      * gen by moon at 8/21/2022, 4:37:45 AM
      * 
      **/
     @Trace(operationName = "M-查用户角色缓存Redis列表（特殊方法）")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public QueryUserRoleRedisListRespDto queryUserRoleRedisList(QueryUserRoleRedisListReqDto reqDto) {
       // TODO ruizhe skai dong ; 8/21/2022, 4:37:45 AM
        return new QueryUserRoleRedisListRespDto();
      }
/**
      * code:getUserRoleListFromRedis
      * name:1-2-08查询用于权限角色列表
      * desc:undefined
      * gen by moon at 9/4/2022, 2:55:05 AM
      * 
      **/
     @Trace(operationName = "1-2-08查询用于权限角色列表")
     @Tags({@Tag(key = "参数",value = "arg[0]"),
     @Tag(key = "返回值",value = "returnedObj")})
      public QueryRoleOfPowerListRespDto queryRoleOfPowerList(QueryRoleOfPowerListReqDto reqDto) {
         QueryRoleOfPowerListRespDto retData = new QueryRoleOfPowerListRespDto();
//         BaseUserInfoForRedis retInfo = CommonFunctionHelper.getUserBasicInfoFromRedis(redisClient,reqDto.getOriginalRoleMemberId());
//         List<PrivacyInfoForRedis> powerList = retInfo.getPowerCodeList();
//         for(PrivacyInfoForRedis onePower : powerList){
//             if(onePower.getPrivacyCode().equals(reqDto.getPrivacyCode())){
//                 retData.setPrivacyCode(reqDto.getPrivacyCode());
//             }
//         }
         return retData;
      }
}
